<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\model\Information;

class InformationController extends Controller
{
	protected $types = ['1' => "防拐防骗",'2' => '寻人方法','3'=>'政策法规','4'=>'成功案例'];

    public function index(Request $request)
    {
    	$label = '资讯管理';
    	$types = $this->types;
    	if ($request->ajax()) {
    		//后台用户列表接口
            $page = $request->page ? $request->page : 1;
            $limit = $request->limit ? $request->limit : 10;
            $offset = ($page - 1) * $limit;
            $count = Information::count('id');
    
            $Infmsg = Information::limit($limit)->offset($offset)->orderBy("publish_time",'desc')->get();

            if (!empty($Infmsg)) 
            {
                foreach ($Infmsg as $key => &$value) 
                {
                	$value['typename'] = $types[$value['types']];
                    $value['publish_time'] = date('Y-m-d H:i:s', $value['publish_time']);
                    unset($value);
                }
            }
            return $this->return_ajax_data($Infmsg,'','',$count);
    	}
    	return view('admin.information.index',compact('label','types'));
    }

    public function addInformat(Request $request)
    {
    	$data = $request->all();
        $is_recommend = !empty($data['is_recommend']) ? $data['is_recommend'] : 0;
        $publish_time = time();

        $save_inf = new Information;
        $save_inf->titles = $data['titles'];
        $save_inf->types = $data['informat_type'];
        $save_inf->contents = $data['contents'];
        $save_inf->pictures = $data['imgurl'];
        $save_inf->video_url = $data['video_url'];
        $save_inf->is_recommend = $is_recommend;
        $save_inf->publish_time = $publish_time;
        if($save_inf->save())
        {
            return $this->call_back_status($save_inf);
        }
    }

    public function update(Request $request)
    {
    	$data = $request->all();    
        $update_data = [
                        'titles'=>$data['titles'],
                        'types'=>$data['informat_type'],
                        'contents'=>$data['contents'],
                        'pictures'=>$data['imgurl'],
                         'video_url'=>$data['video_url'],
                        ];
        $update_status = Information::where('id','=',$data['id'])->update($update_data);
        return $this->call_back_status($update_status);
    }

    public function delInformat(Request $request)
    {
    	$id = $request->id;
        $status = Information::where('id','=',$id)->delete();
        return $this->call_back_status($status);
    }

    public function isRecommend(Request $request)
    {
    	$data = $request->all();
    	$name = $data['types'] == 1 ? 'attention' : 'is_recommend';
    	
        $status = Information::where('id','=',$data['id'])->update([$name=>$data[$name]]);
        return $this->call_back_status($status);
    }

}
